<?php
// namespace Fn\pdfWatermark;
// use Fn\pdfWatermark\fpdf\FPDF;
// use Fn\pdfWatermark\fpdi\FPDI;
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');

class Word{ 
    function setPdfWatermark($path,$char,$x = 95,$y = 31,$fontSize = '12'){   
        //word_watermark
        $pdf = new FPDI();

        // get the page count
        $pageCount = $pdf->setSourceFile($path);
        
        // file_put_contents($path, file_get_contents($path));die();
        // iterate through all pages
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
        {
            // import a page
            $templateId = $pdf->importPage($pageNo);

            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);

            // create a page (landscape or portrait depending on the imported page size)
            if ($size['w'] > $size['h']) $pdf->AddPage('L', array($size['w'], $size['h']));
            else $pdf->AddPage('P', array($size['w'], $size['h']));

            // use the imported page
            $pdf->useTemplate($templateId);
            $pdf->SetMargins(1,1,1);

            $pdf->SetFont('Arial','B',$fontSize);
            // sign with current date
            $pdf->SetXY($x, $y); // you should keep testing untill you find out correct x,y values
            $pdf->Write(0, $char);

        }
        $pdf->Output($path);
    }

    function addPdf($path,$data,$productInfoRes,$senderInfo,$customs){ 
        //word_watermark
        $pdf = new FPDI();
        // $pdfFont = new PDF_Chinese();
        // get the page count
      /*  $pageCount = $pdf->setSourceFile($path);
        
        // file_put_contents($path, file_get_contents($path));die();
        // iterate through all pages
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
        {
            // import a page
            $templateId = $pdf->importPage($pageNo);

            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);

            // create a page (landscape or portrait depending on the imported page size)
            if ($size['w'] > $size['h']) $pdf->AddPage('L', array($size['w'], $size['h']));
            else $pdf->AddPage('P', array($size['w'], $size['h']));

            // use the imported page
            $pdf->useTemplate($templateId);
            $pdf->SetMargins(1,1,1);

            $pdf->SetFont('Arial','B',$fontSize);
            // sign with current date
            $pdf->SetXY($x, $y); // you should keep testing untill you find out correct x,y values
            // $pdf->Write(0, $char);

        }*/
        
        // if ($size['w'] > $size['h']) {
        //     $pdf->AddPage('L', array($size['w'], $size['h']));
        // }else {
        //     $pdf->AddPage('P', array($size['w'], $size['h']));
        // }
        $pdf->AddPage('P','A4');
        $pdf->SetMargins(1,1,1);
   
        $pdf->SetFont('Arial','B',12);
        $pdf->Text(70,10," Proforma Invoice ");
        $pdf->Text(10,20,'Airway Bill No.: '.$data['orders_tn']);
        $pdf->Text(10,25,'Shipper: ');
        $pdf->Text(10,30,'Company Name: '.$senderInfo['company']);
        $pdf->Text(10,35,'Address: '.$senderInfo['address1']);
        $pdf->Text(10,40,' '.$senderInfo['address2'].$senderInfo['address3']);
        $pdf->Text(10,45,'Contact Name: '.$senderInfo['name']);
        $pdf->Text(10,50,'Phone/Fax: '.$senderInfo['phone']);

        $pdf->Text(10,60,'Consignee:');
        $pdf->Text(10,65,'Company Name: '."none");
        $pdf->Text(10,70,'Address: '.$data['orders_delivery_address']." ".$data['orders_delivery_address2']);
        $pdf->Text(10,75,'City: '.$data['orders_delivery_city']);
        $pdf->Text(10,80,'State/Country: ' .$data['orders_delivery_state']." / " .$data['orders_delivery_country_iso']);
        $pdf->Text(10,85,'Contact name: '.$data['orders_delivery_name']);
        $pdf->Text(10,90,'Phone/Fax: '.$data['orders_delivery_tel']);

        $pdf->Text(10,105,'Full Description');
        $pdf->Text(10,110,'Of Goods');
        $pdf->Text(70,105,'Harmonised ');
        $pdf->Text(70,110,'code (HS Code)');
        $pdf->Text(110,105,' No. of ');
        $pdf->Text(110,110,' Item');
        $pdf->Text(130,105,'Unit Value');
        $pdf->Text(130,110,' (USD)');
        $pdf->Text(160,105,'Total Value');
        $pdf->Text(160,110,' (USD)');
        $start = 125;
        $realPrice = 0;
        foreach ($customs as $key => $value) {
            $start = $start+5;
            $pdf->Text(10,$start,$value['name']);
            $pdf->Text(114,$start,$value['qty']);
            $pdf->Text(134,$start,$value['unitPrice']);
            $pdf->Text(164,$start,$value['totalPrice']);
            $realPrice += $value['totalPrice'];
        }

        $pdf->Text(10,100,"___________________________________________________________________________");
        $pdf->Text(10,122,"___________________________________________________________________________");

        $pdf->Text(10,240,"Total Invoice Value: ".$realPrice);
        $pdf->Text(10,245,"No. of Piece : ".$productInfoRes['realQty']);
        $pdf->Text(10,250,"Total Weight : ".round($productInfoRes['realWeight'],2));
        $pdf->Text(10,255,"Dimensions : ");
        $pdf->Text(10,260,"Reason for Export: "."sample");

        $pdf->Text(10,270,"I declare that the information is true and correct to the best of my knowledge,and ");
        $pdf->Text(10,275,"that the goods are of origin. I (name) certify that the contents of this shipment are ");
        $pdf->Text(10,280,"as stated above.");

        $pdf->Text(90,290,"Signature:");

        $pdf->AddGBFont(); 
        // $pdf->SetXY(55, 10);
        $pdf->SetFont('GB','',12);
        // $pdf->Write(0, iconv("utf-8","gb2312",'形式发票'));
        $pdf->Text(110,10,iconv("utf-8","gb2312",'形式发票'));
        // $pdf->SetXY(10, 72);
        // $pdf->Write(0, iconv("utf-8","gb2312","物品信息描述请"));
        $pdf->Text(10,115,iconv("utf-8","gb2312","物品信息描述请"));
        // $pdf->SetXY(10, 75);
        // $pdf->Write(0, iconv("utf-8","gb2312","详细列明"));
        $pdf->Text(10,120,iconv("utf-8","gb2312","详细列明"));
        // $pdf->SetXY(35, 72);
        // $pdf->Write(0, iconv("utf-8","gb2312","商品税则编码"));
        $pdf->Text(73,115,iconv("utf-8","gb2312","商品税则编码"));
        // $pdf->SetXY(57, 72);
        // $pdf->Write(0, iconv("utf-8","gb2312","数量"));
        $pdf->Text(113,115,iconv("utf-8","gb2312","数量"));
        // $pdf->SetXY(66, 72);
        // $pdf->Write(0, iconv("utf-8","gb2312","单价"));
        $pdf->Text(133,115,iconv("utf-8","gb2312","单价"));
        // $pdf->SetXY(80, 72);
        // $pdf->Write(0, iconv("utf-8","gb2312","总价"));
        $pdf->Text(163,115,iconv("utf-8","gb2312","总价"));

        $pdf->Text(50,280,iconv("utf-8","gb2312","(声明)"));
        $pdf->Text(110,290,iconv("utf-8","gb2312"," 发件人签名/盖公章：Bob"));
 
        $pdf->Output($path);
    }
}


